package
{
	import flash.utils.Dictionary;
	
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxSprite;
	import org.flixel.FlxText;

	public class CheatSheet extends FlxGroup
	{
	
		private var rules:FlxGroup;
		private var levelToElementMap:Dictionary;
		private var elementToImgMap:Dictionary;
		public var sheet:FlxSprite;
		private var backdrop:FlxSprite;
		private var tutorialSheet:FlxSprite;
		private var tutorialRule:FlxSprite;
		private var tutorialText:FlxText;

		private var currentY:int;
		private static const START_Y:int = 32;
		
		
		public function CheatSheet(startLevel:int) {
			backdrop = new FlxSprite(0, 0, Res.Backdrop);
			add(backdrop);
			
			// Actual big sheet
			sheet = new FlxSprite(0,0, Res.CheatPaper);
			sheet.x = (FlxG.width - sheet.width) / 2;
			sheet.y = (FlxG.height - sheet.height) / 2;
			add(sheet);
			
			// Tiny sheet for tutorials.
			tutorialSheet = new FlxSprite();
			tutorialSheet.makeGraphic(400, 100);
			tutorialSheet.drawRect(0, 0, 400, 100, 0xFFFFFF);
			tutorialSheet.x = (FlxG.width - 400) / 2;
			tutorialSheet.y = (FlxG.height - 60) / 2;
			
			tutorialRule = new FlxSprite();
			tutorialRule.x = tutorialSheet.x + 20;
			tutorialRule.y = tutorialSheet.y + 50;
			
			tutorialText = new FlxText(tutorialSheet.x + 10, tutorialSheet.y + 10, 400-20, "", false);
			tutorialText.setFormat("Georgia", 24, 0x000000, "left");
			
			rules = new FlxGroup();
			add(rules);

			levelToElementMap = new Dictionary();
			levelToElementMap[5] = "MUD";
			levelToElementMap[8] = "FIRE";
			levelToElementMap[9] = "ICE";
			levelToElementMap[10] = "STEAM";
			levelToElementMap[11] = "LIGHTNING";
			levelToElementMap[15] = "SAND";
			levelToElementMap[20] = "METAL";
			levelToElementMap[22] = "WOOD";
			levelToElementMap[25] = "GLASS";
			levelToElementMap[27] = "RAINBOW";
			levelToElementMap[35] = "VOID";
			
			elementToImgMap = new Dictionary();
			elementToImgMap["MUD"] = Res.MudRule;
			elementToImgMap["FIRE"] = Res.FireRule;
			elementToImgMap["ICE"] = Res.IceRule;
			elementToImgMap["STEAM"] = Res.SteamRule;
			elementToImgMap["LIGHTNING"] = Res.LightningRule;
			elementToImgMap["SAND"] = Res.SandRule;
			elementToImgMap["METAL"] = Res.MetalRule;
			elementToImgMap["WOOD"] = Res.WoodRule;
			elementToImgMap["GLASS"] = Res.GlassRule;
			elementToImgMap["RAINBOW"] = Res.RainbowRule;
			elementToImgMap["VOID"] = Res.VoidRule;
			
			currentY = START_Y;
			
			addAllRulesUpToLevel(startLevel);
		}
		
		/**
		 * Append element rule to cheat sheet (corresponding to level)
		 */
		public function addRuleByLevel(level:int):void {
			var rule:FlxSprite = new FlxSprite();
			rule.loadGraphic(elementToImgMap[levelToElementMap[level]]);
			rule.x = 20 + sheet.x;
			rule.y = currentY + sheet.y;
			currentY += 40;
			rules.add(rule);
		}
		
		
		/**
		 * Populate the cheat sheet with all rules up to the specified
		 * level
		 */
		public function addAllRulesUpToLevel(level:int):void {
			for (var i:int = 0; i <= level; i++) {
				if (levelToElementMap[i] != null) {
					addRuleByLevel(i);
				}
			}
		}
		
		/**
		 * Intended to be called before the parent class adds this to screen.
		 * Prepares the correct sheet to be shown to screen, depending on whether
		 * we want to see a specific tutorial or not.
		 */
		public function prepare(tutorial:Boolean, level:int):void {
			if (tutorial) {
				remove(sheet);
				remove(rules);
				
				add(tutorialSheet);
				tutorialRule.loadGraphic(elementToImgMap[levelToElementMap[level]]);
				add(tutorialRule);
				tutorialText.text = "Combination for " + levelToElementMap[level];
				add(tutorialText);
			} else {
				remove(tutorialSheet);
				remove(tutorialRule);
				remove(tutorialText);
				
				add(sheet);
				add(rules);
			}
		}
		
		public function overlapsPoint(x:Number, y:Number):Boolean {
			return backdrop.overlapsPoint(x, y) || sheet.overlapsPoint(x, y);
		}
		
		/**
		 * Returns true if this level does have a rule tutorial.
		 */
		public function containsRuleForLevel(level:int):Boolean {
			return levelToElementMap[level] != null;
		}
	}
}